//
//  UIImage+Extension.swift
//  图片选择器
//
//  Created by LiHong on 15/11/8.
//  Copyright © 2015年 LiHong. All rights reserved.
//

import UIKit

extension UIImage {
    
    func scaleImage() -> UIImage {
        let newWidth: CGFloat = 300
        
        if size.width < newWidth {
            return self
        }
        
        let newHeight = newWidth * size.height / size.width
        
        let newSize = CGSize(width: newWidth, height: newHeight)
        
        UIGraphicsBeginImageContext(newSize)
        
        drawInRect(CGRect(x: 0, y: 0, width: newWidth, height: newHeight))
        
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext()
        
        return newImage
    }
}
